package com.toweave.starter.activity;

import static com.toweave.starter.utils.ConstantsUtil.LIZ_TAG;

import androidx.appcompat.app.AppCompatActivity;
import androidx.databinding.DataBindingUtil;

import android.os.Bundle;
import android.util.Log;
import android.view.View;

import com.toweave.starter.R;
import com.toweave.starter.User;
import com.toweave.starter.databinding.ActivityBindingTestBinding;

import java.util.HashMap;
import java.util.Locale;
import java.util.Map;


public class BindingTestActivity extends AppCompatActivity implements View.OnClickListener {

  private ActivityBindingTestBinding binding;

  // 初始化 HashMap 的 Runnable 对象
  Map<Integer, Runnable> functions = new HashMap<>();

  @Override
  protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    // binding = ActivityBindingTestBinding.inflate(getLayoutInflater());
    // View view = binding.getRoot();
    // setContentView(view);

    binding = DataBindingUtil.setContentView(this, R.layout.activity_binding_test);

    User user = new User("Test", "User");
    binding.setUser(user);

    binding.tvBindingTest.setText("Binding Test");
    binding.tvBindingTest.setOnClickListener(this);
  }

  @Override
  protected void onDestroy() {
    super.onDestroy();
    binding = null;
  }

  @Override
  public void onClick(View v) {
    int id = v.getId();
    // 点击 tvBindingTest 时，执行的任务、方法
    functions.put(binding.tvBindingTest.getId(), () -> {
      String str = String.format(Locale.CHINA, "Hello: %d", binding.tvBindingTest.getId());
      Log.i(LIZ_TAG, str);
    });
    // 检查HashMap中是否有对应的Runnable对象
    Runnable task = functions.get(id);
    if (task != null) {
      task.run();
    } else {
      // 处理不存在映射的情况，比如打印日志
      Log.w(LIZ_TAG, "No task found for the ID: " + id);
    }
  }
}

